﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Terraria.ModLoader.IO;

namespace Terraria.ModLoader.Default
{
	// Token: 0x02000303 RID: 771
	internal class MysteryTilesWorld : ModWorld
	{
		// Token: 0x06001D5E RID: 7518 RVA: 0x00014D08 File Offset: 0x00012F08
		public override void Initialize()
		{
			this.infos.Clear();
			this.pendingInfos.Clear();
		}

		// Token: 0x06001D5F RID: 7519 RVA: 0x00438E1C File Offset: 0x0043701C
		public override TagCompound Save()
		{
			TagCompound tagCompound = new TagCompound();
			tagCompound["list"] = (from info in this.infos
			select ((info != null) ? info.Save() : null) ?? new TagCompound()).ToList<TagCompound>();
			return tagCompound;
		}

		// Token: 0x06001D60 RID: 7520 RVA: 0x00438E68 File Offset: 0x00437068
		public override void Load(TagCompound tag)
		{
			List<ushort> list = new List<ushort>();
			bool flag = false;
			foreach (TagCompound tagCompound in tag.GetList<TagCompound>("list"))
			{
				if (!tagCompound.ContainsKey("mod"))
				{
					this.infos.Add(null);
					list.Add(0);
				}
				else
				{
					string @string = tagCompound.GetString("mod");
					string string2 = tagCompound.GetString("name");
					MysteryTileInfo item = tagCompound.ContainsKey("frameX") ? new MysteryTileInfo(@string, string2, tagCompound.GetShort("frameX"), tagCompound.GetShort("frameY")) : new MysteryTileInfo(@string, string2);
					this.infos.Add(item);
					Mod mod = ModLoader.GetMod(@string);
					int num = (mod != null) ? mod.TileType(string2) : 0;
					list.Add((ushort)num);
					if (num != 0)
					{
						flag = true;
					}
				}
			}
			if (flag)
			{
				this.RestoreTiles(list);
				for (int i = 0; i < list.Count; i++)
				{
					if (list[i] > 0)
					{
						this.infos[i] = null;
					}
				}
			}
			if (this.pendingInfos.Count > 0)
			{
				this.ConfirmPendingInfo();
			}
		}

		// Token: 0x06001D61 RID: 7521 RVA: 0x00438FBC File Offset: 0x004371BC
		public override void LoadLegacy(BinaryReader reader)
		{
			List<TagCompound> list = new List<TagCompound>();
			int num = reader.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				string text = reader.ReadString();
				if (text.Length == 0)
				{
					list.Add(new TagCompound());
				}
				else
				{
					TagCompound tagCompound = new TagCompound();
					tagCompound["mod"] = text;
					tagCompound["name"] = reader.ReadString();
					TagCompound tagCompound2 = tagCompound;
					if (reader.ReadBoolean())
					{
						tagCompound2.Set("frameX", reader.ReadInt16());
						tagCompound2.Set("frameY", reader.ReadInt16());
					}
					list.Add(tagCompound2);
				}
			}
			TagCompound tagCompound3 = new TagCompound();
			tagCompound3["list"] = list;
			this.Load(tagCompound3);
		}

		// Token: 0x06001D62 RID: 7522 RVA: 0x00439080 File Offset: 0x00437280
		private void RestoreTiles(List<ushort> canRestore)
		{
			ushort num = (ushort)ModLoader.GetMod("ModLoader").TileType("MysteryTile");
			for (int i = 0; i < Main.maxTilesX; i++)
			{
				for (int j = 0; j < Main.maxTilesY; j++)
				{
					if (Main.tile[i, j].type == num)
					{
						Tile tile = Main.tile[i, j];
						MysteryTileFrame mysteryTileFrame = new MysteryTileFrame(tile.frameX, tile.frameY);
						int frameID = mysteryTileFrame.FrameID;
						if (canRestore[frameID] > 0)
						{
							MysteryTileInfo mysteryTileInfo = this.infos[frameID];
							tile.type = canRestore[frameID];
							tile.frameX = mysteryTileInfo.frameX;
							tile.frameY = mysteryTileInfo.frameY;
						}
					}
				}
			}
		}

		// Token: 0x06001D63 RID: 7523 RVA: 0x00439150 File Offset: 0x00437350
		private void ConfirmPendingInfo()
		{
			List<int> list = new List<int>();
			int num = 0;
			for (int i = 0; i < this.pendingInfos.Count; i++)
			{
				while (num < this.infos.Count && this.infos[num] != null)
				{
					num++;
				}
				if (num == this.infos.Count)
				{
					this.infos.Add(this.pendingInfos[i]);
				}
				else
				{
					this.infos[num] = this.pendingInfos[i];
				}
				list.Add(num);
			}
			ushort num2 = (ushort)ModLoader.GetMod("ModLoader").TileType("PendingMysteryTile");
			ushort type = (ushort)ModLoader.GetMod("ModLoader").TileType("MysteryTile");
			for (int j = 0; j < Main.maxTilesX; j++)
			{
				for (int k = 0; k < Main.maxTilesY; k++)
				{
					if (Main.tile[j, k].type == num2)
					{
						Tile tile = Main.tile[j, k];
						MysteryTileFrame mysteryTileFrame = new MysteryTileFrame(tile.frameX, tile.frameY);
						mysteryTileFrame = new MysteryTileFrame(list[mysteryTileFrame.FrameID]);
						tile.type = type;
						tile.frameX = mysteryTileFrame.FrameX;
						tile.frameY = mysteryTileFrame.FrameY;
					}
				}
			}
		}

		// Token: 0x040018F3 RID: 6387
		private List<MysteryTileInfo> infos = new List<MysteryTileInfo>();

		// Token: 0x040018F4 RID: 6388
		internal List<MysteryTileInfo> pendingInfos = new List<MysteryTileInfo>();
	}
}
